package controller;

import com.alibaba.fastjson.JSON;
import common.jsondata.IResponseJSONResult;
import common.jsondata.impl.ResponseJSONResultImpl;
import entity.TreeNode;
import entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserService;
import util.TreeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/gets")
public class MenuResourceController {

    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    private static IResponseJSONResult responseJSONResult = new ResponseJSONResultImpl();
    @Autowired
    private UserService userService;
    //跳转菜单页面
    @RequestMapping("/menu")
    public String menu(){
        return  "views/user/menumange";
    }

    //查看菜单列表详情
    @RequestMapping("/menudetail")
    public String menuDetail(){ return  "views/user/menudetail"; }

    //编辑菜单
    @RequestMapping("/modifymenu")
    public String modifymenu(){ return  "views/user/modifymenu"; }

    @ResponseBody
    @RequestMapping("/tree")
    public TreeNode getMenuTree(){

        List<TreeNode> treeNodeList = userService.findResource();
        logger.info("资源：" + JSON.toJSONString(treeNodeList));

        TreeUtil treeUtil = new TreeUtil(treeNodeList);
        TreeNode treeNode = treeUtil.generateTree(0);
        logger.info("treeNode" + JSON.toJSONString(treeNode));
        return treeNode;
    }

    /**
     * 返回资源树的列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public String  getTreeList(HttpServletRequest request, HttpServletResponse response){
        List<TreeNode> list = userService.findMenuList();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("count",list.size());
        map.put("data",list);
        logger.debug("数据：" + JSON.toJSONString(map) );
        responseJSONResult.setResponseUTF8(response);
        return JSON.toJSONString(map);
    }

    /**
     * 删除菜单信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteMenu")
    public String  deleteMenu(HttpServletRequest request, HttpServletResponse response){
        logger.info("删除菜单信息");
        return JSON.toJSONString("");
    }

}
